package com.tpyule.tpclass.web.Controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.tpyule.tpclass.entity.bean.FailResultBean;
import com.tpyule.tpclass.entity.bean.ResultBean;
import com.tpyule.tpclass.entity.bean.SuccessResultBean;
import com.tpyule.tpclass.entity.bo.MenuDetailBo;
import com.tpyule.tpclass.entity.pojo.Answer;
import com.tpyule.tpclass.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @author zzz
 * @version 1.0
 * @date 2020/5/5 10:36
 */
@RestController
@RequestMapping("menu")
public class MenuController extends BaseController {
    @Autowired
    MenuService menuService;

    @RequestMapping("/add")
    @ResponseBody
    public void insertMenu(HttpServletRequest request, HttpServletResponse response){
//        ObjectMapper objectMapper = new ObjectMapper();
//        ResultBean resultBean = null;
//        try {
//            String answerData = request.getParameter("answerData");
//            if (answerData!=null){
//                Answer answer = objectMapper.readValue(answerData, Answer.class);
//                if (answer!=null){
//                    int result = answerService.addAnswer(answer);
//                    if (result!=0){
//                        resultBean = new SuccessResultBean();
//                    } else {
//                        resultBean = new FailResultBean();
//                    }
//                } else {
//                    resultBean = new FailResultBean("数据错误");
//                }
//            } else {
//                resultBean = new FailResultBean("数据错误");
//            }
//        } catch (JsonProcessingException e) {
//            e.printStackTrace();
//        } finally {
//            this.setAjaxResponseResult(response, resultBean);
//        }
    }


    @RequestMapping("/getListMenu")
    @ResponseBody
    public void getListMenu(HttpServletRequest request, HttpServletResponse response) throws IOException {
        ObjectMapper objectMapper = new ObjectMapper();
        ResultBean resultBean = null;
        try {
            String roleId = request.getParameter("roleId");
            if (roleId!=null){
                int rid = Integer.parseInt(roleId);
                List<MenuDetailBo> bos = menuService.getListMenu(rid);
                resultBean = new SuccessResultBean(bos, "成功");
            } else {
                resultBean = new FailResultBean("数据错误");
            }
        } catch (Exception e) {
            resultBean = new FailResultBean("错误");
        } finally {
            this.setAjaxResponseResult(response, resultBean);
        }
    }
}
